package com.starhub.domain.ai.controller;

import com.starhub.application.message.result.AIChatMessageResult;
import com.starhub.application.message.result.AIChatSessionResult;
import com.starhub.application.message.service.IMessageStoreService;
import com.starhub.common.bean.ResultResp;
import com.starhub.common.security.util.SecurityUtils;
import com.starhub.common.sys.user.LoginUser;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 消息管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/starhub/message")
public class MessageController {

    @Autowired
    private IMessageStoreService messageStoreService;

    /**
     * 获取用户的会话列表（分页）
     *
     * @param agentId 代理ID
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 会话列表
     */
    @GetMapping("/sessions")
    public ResultResp<AIChatSessionResult> getSessions(
            @RequestParam Long agentId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            Long userId = user.getUser().getId();
            return ResultResp.success(messageStoreService.getUserSessions(userId, agentId,pageNum, pageSize));
        } catch (Exception e) {
            log.error("chat error", e);
            return ResultResp.fail(e.getMessage(),e);
        }
    }

    /**
     * 获取指定会话的消息列表（分页）
     *
     * @param pageNum 页码
     * @param pageSize 每页大小
     * @return 消息列表
     */
    @GetMapping("/messages")
    public ResultResp<AIChatMessageResult> getSessionMessages(
            @RequestParam Long chatId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "100") Integer pageSize) {
        try {
            LoginUser user = SecurityUtils.getCurrentUser();
            Long userId = user.getUser().getId();
            return ResultResp.success(messageStoreService.getSessionMessages(userId, chatId, pageNum, pageSize));
        } catch (Exception e) {
            log.error("chat error", e);
            return ResultResp.fail(e.getMessage(),e);
        }
    }
} 